package com.dxd.onlineshop.pojo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    private Integer orderId; //主键
    private Integer userId; //所属用户
    private String orderNumber; //订单编号
    private BigDecimal totalAmount; //应付金额
    private OrderStatus orderStatus; //订单状态

    //订单状态
    @SuppressWarnings("NonAsciiCharacters")
    public enum OrderStatus {
        待付款, 已付款, 已发货, 已完成, 已取消
    }

    //订单创建时间
    @Builder.Default
    private LocalDateTime createdAt = LocalDateTime.now();

    //订单更新时间
    @Builder.Default
    private LocalDateTime updatedAt = LocalDateTime.now();

    //订单项
    @Builder.Default
    private List<OrderItem> orderItems = new ArrayList<>();



    //添加商品到订单
    public void addOrderItem(OrderItem item) {
        orderItems.add(item);
    }

    //更新订单应付金额
    public void updateTotalAmount() {
        this.totalAmount = orderItems.stream()
                .map(OrderItem::getSubtotal)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}